package com.das.mechanic_base.adapter.record;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.bean.record.RecordKeyBean;
import com.das.mechanic_base.utils.X3StringUtils;
import com.das.mechanic_base.widget.X3PointDialog;

import java.util.ArrayList;
import java.util.List;

public class X3RecordListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;

    private List<RecordKeyBean> mList;

    /**
     * 空数据时，显示空布局类型
     */
    private final int EMPTY_VIEW = 1;
    /**
     * 控制空布局的显隐
     */
    private int mEmptyType = 0;

    private List<Boolean> sList;

    private List<RecordKeyBean> selectList;
    private final X3PointDialog delDialog;

    public X3RecordListAdapter(Context mContext) {
        this.mContext = mContext;
        mList = new ArrayList<>();
        sList = new ArrayList<>();
        selectList = new ArrayList<>();
        delDialog = new X3PointDialog((Activity) mContext, "", mContext.getString(R.string.x3_affirm_delete_record), mContext.getString(R.string.x3_cancel_info), mContext.getString(R.string.x3_affirm_info));
    }

    public void changeRecordList(List<RecordKeyBean> workList) {
        this.mList = workList;
        if (X3StringUtils.isListEmpty(mList)) {
            mEmptyType = 1;
        }else {
            mEmptyType = 0;
            for (int i = 0; i < mList.size(); i++) {
                sList.add(false);
            }
        }
        notifyDataSetChanged();
    }

    /**
     * 转写录音文件 显示内容
     * @param workList
     * @param index
     */
    public void changeCurrentRecord(List<RecordKeyBean> workList,int index) {
        this.mList = workList;
        notifyItemChanged(index);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        switch (viewType) {
            case 0:
                return  new RecordListHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_record_list_item,parent,false));
            case 1:
                return new EmptyHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_empty_record_item, parent, false));
        }
        return null;
    }

    /**
     * 是否全部选中
     * @param isSelect
     */
    public void isAllSelect(boolean isSelect) {
        sList.clear();
        for (int i = 0; i < mList.size(); i++) {
            sList.add(isSelect);
        }
        notifyDataSetChanged();
        selectList.clear();
        if (isSelect) {
            selectList.addAll(mList);
        }

        if (iOnClickRefresh != null) {
            iOnClickRefresh.iOnClickRefreshData(selectList);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (mEmptyType == EMPTY_VIEW) {
            //空布局的类型
            return 1;
        }
        return 0;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder == null) {
            return;
        }
        int itemViewType = getItemViewType(position);
        if (itemViewType == 0) {
            RecordListHolder recordListHolder = (RecordListHolder) holder;

            if (sList.get(position)) {
                recordListHolder.iv_select.setImageResource(R.mipmap.x3_iv_task_status_select);
            }else {
                recordListHolder.iv_select.setImageResource(R.mipmap.x3_iv_task_status_unsele);
            }

            long receiveBaseId = mList.get(position).receiveBaseId;
            if (receiveBaseId > 0) {
                recordListHolder.tv_no_upload.setVisibility(View.GONE);
            }else {
                recordListHolder.tv_no_upload.setVisibility(View.VISIBLE);
            }

            recordListHolder.tv_title.setText(mList.get(position).soundKey);
            recordListHolder.tv_time.setText(mList.get(position).startTime);
            String resourceContent = mList.get(position).resourceContent;
            if (X3StringUtils.isEmpty(resourceContent)) {
                recordListHolder.tv_content.setVisibility(View.GONE);
            }else {
                recordListHolder.tv_content.setVisibility(View.VISIBLE);
                recordListHolder.tv_content.setText(resourceContent);
            }

            recordListHolder.iv_select.setOnClickListener(v -> {
                if (sList.get(position)) {
                    sList.remove(position);
                    sList.add(position,false);
                }else {
                    sList.remove(position);
                    sList.add(position,true);
                }

                selectList.clear();
                for (int i = 0; i < mList.size(); i++) {
                    if (sList.get(i)) {
                        selectList.add(mList.get(i));
                    }
                }
                notifyItemChanged(position);
                if (iOnClickRefresh != null) {
                    iOnClickRefresh.iOnClickRefreshData(selectList);
                }
            });

            recordListHolder.rl_main.setOnClickListener(v -> {
                if (iOnClickRefresh != null) {
                    iOnClickRefresh.iOnCheckFileMp3(mList.get(position),resourceContent);
                }
            });

            recordListHolder.tv_right.setOnClickListener(v -> {
                // 删除
                if (mList.get(position).receiveBaseId <= 0) {
                    // 说明还未同步
                    delDialog.show();
                    delDialog.setiBtnClick(new X3PointDialog.IBtnClick() {
                        @Override
                        public void iBtnAffirmClick() {
                            if (iOnClickRefresh != null) {
                                iOnClickRefresh.iOnDeleteFile(mList.get(position));
                            }
                            mList.remove(position);
                            notifyItemRemoved(position);//刷新被删除的地方
                            notifyDataSetChanged();

                            selectList.clear();
                            for (int i = 0; i < mList.size(); i++) {
                                if (sList.get(i)) {
                                    selectList.add(mList.get(i));
                                }
                            }
                            if (iOnClickRefresh != null) {
                                iOnClickRefresh.iOnClickRefreshData(selectList);
                            }
                        }

                        @Override
                        public void iBtnCancelClick() {}
                    });
                    return;
                }

                if (iOnClickRefresh != null) {
                    iOnClickRefresh.iOnDeleteFile(mList.get(position));
                }
                mList.remove(position);
                notifyItemRemoved(position);//刷新被删除的地方
                notifyDataSetChanged();

                selectList.clear();
                for (int i = 0; i < mList.size(); i++) {
                    if (sList.get(i)) {
                        selectList.add(mList.get(i));
                    }
                }
                if (iOnClickRefresh != null) {
                    iOnClickRefresh.iOnClickRefreshData(selectList);
                }

            });

        }else {
            EmptyHolder emptyHolder = (EmptyHolder) holder;
            emptyHolder.tv_empty.setText(mContext.getString(R.string.x3_go_to_record));
        }
    }

    IOnClickRefresh iOnClickRefresh;

    public void setiOnClickRefresh(IOnClickRefresh iOnClickRefresh) {
        this.iOnClickRefresh = iOnClickRefresh;
    }

    public interface IOnClickRefresh {
        /**
         * 刷新选中的数据
         * @param recordListBeans
         */
        void iOnClickRefreshData(List<RecordKeyBean> recordListBeans);

        /**
         * 删除当前文件
         * @param recordListBean
         */
        void iOnDeleteFile(RecordKeyBean recordListBean);

        /**
         * 查看本地录音文件以及转换录音
         * @param recordListBean
         */
        void iOnCheckFileMp3(RecordKeyBean recordListBean,String content);
    }


    @Override
    public int getItemCount() {
        if (X3StringUtils.isListEmpty(mList)) {
            return mEmptyType;
        }
        return mList.size();
    }

    class RecordListHolder extends RecyclerView.ViewHolder {
        TextView tv_title;
        TextView tv_content;
        TextView tv_time;
        ImageView iv_select;
        TextView tv_right;
        TextView tv_no_upload;
        RelativeLayout rl_main;
        public RecordListHolder(@NonNull View itemView) {
            super(itemView);
            tv_title = itemView.findViewById(R.id.tv_title);
            tv_content = itemView.findViewById(R.id.tv_content);
            tv_time = itemView.findViewById(R.id.tv_time);
            iv_select = itemView.findViewById(R.id.iv_select);
            tv_right = itemView.findViewById(R.id.tv_right);
            tv_no_upload = itemView.findViewById(R.id.tv_no_upload);
            rl_main = itemView.findViewById(R.id.rl_main);
        }
    }

    public static class EmptyHolder extends RecyclerView.ViewHolder {
        public TextView tv_empty;
        public EmptyHolder(@NonNull View itemView) {
            super(itemView);
            tv_empty = itemView.findViewById(R.id.tv_empty);
        }
    }
}
